﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace btnet.pages
{
    public partial class svn_diff : CommonPage
    {
        protected string left_out = "";
        protected string right_out = "";
        protected string unified_diff_text = "";
        protected string left_title = "";
        protected string right_title = "";
        protected string path = "";
        protected string repo;

        protected void Page_Load(object sender, EventArgs e)
        {
            Bug_Util.do_not_cache(Response);

            security = new Security();
            security.check_security(HttpContext.Current, Security.ANY_USER_OK);

            string sql = @"
select svnrev_revision, svnrev_repository, svnap_path, svnrev_bug
from svn_revisions
inner join svn_affected_paths on svnap_svnrev_id = svnrev_id
where svnap_id = $id";

            int svnap_id = Convert.ToInt32(Bug_Util.sanitize_integer(Request["id"]));
            sql = sql.Replace("$id", Convert.ToString(svnap_id));

            DataRow dr = DbUtil.get_datarow(sql);

            // check if user has permission for this bug
            int permission_level = Bug.get_bug_permission_level((int)dr["svnrev_bug"], security);
            if (permission_level == Security.PERMISSION_NONE)
            {
                Response.Write("You are not allowed to view this item");
                Response.End();
            }

            repo = (string)dr["svnrev_repository"];
            path = (string)dr["svnap_path"];

            string error = "";

            string string_revision0 = Request["rev_0"];

            if (string.IsNullOrEmpty(string_revision0))
            {
                int revision = (int)dr["svnrev_revision"];

                unified_diff_text = VersionControl.svn_diff(repo, path, revision, 0);
                examine_diff(unified_diff_text);

                // get the old revision number
                Regex regex = new Regex("\n");
                string[] diff_lines = regex.Split(unified_diff_text.Replace("\r\n", "\n"));

                string line = diff_lines[2];
                int old_rev_pos1 = line.ToLower().IndexOf("(revision ");  // 10 chars long
                int old_rev_pos_start_of_int = old_rev_pos1 + 10;
                int old_rev_after_int = line.IndexOf(")", old_rev_pos_start_of_int);
                string old_revision_string = line.Substring(old_rev_pos_start_of_int,
                    old_rev_after_int - old_rev_pos_start_of_int);

                int old_revision = Convert.ToInt32(old_revision_string);


                // get the source code for both the left and right
                string left_text = VersionControl.svn_cat(repo, path, old_revision);
                string right_text = VersionControl.svn_cat(repo, path, revision);
                left_title = Convert.ToString(old_revision);
                right_title = Convert.ToString(revision);

                error = VersionControl.visual_diff(unified_diff_text, left_text, right_text, ref left_out, ref right_out);

            }
            else
            {


                int revision1 = Convert.ToInt32(Request["rev_1"]);
                int revision0 = Convert.ToInt32(string_revision0);

                unified_diff_text = VersionControl.svn_diff(repo, path, revision1, revision0);
                examine_diff(unified_diff_text);

                // get the source code for both the left and right
                string left_text = VersionControl.svn_cat(repo, path, revision0);
                string right_text = VersionControl.svn_cat(repo, path, revision1);
                left_title = Convert.ToString(revision0);
                right_title = Convert.ToString(revision1);

                error = VersionControl.visual_diff(unified_diff_text, left_text, right_text, ref left_out, ref right_out);

            }

            if (error != "")
            {
                Response.Write(error);
                Response.End();
            }

        }

        ///////////////////////////////////////////////////////////////////////
        void examine_diff(string unified_diff_text)
        {

            if (unified_diff_text == "")
            {
                Response.Write("No differences.");
                Response.End();
            }

            int error_pos = unified_diff_text.IndexOf("Cannot display: file marked as a binary type.");
            if (error_pos > -1)
            {
                Response.Write("<div style='color:red; font-weight: bold; font-size: 10pt;>");
                Response.Write(unified_diff_text.Substring(error_pos));
                Response.Write("<br>Subversion thinks this is a binary file.</div>");
                Response.End();
            }
        }
    }
}
